package com.micro.frog.fortune.executor.cwl;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.micro.frog.fortune.dao.entity.CwlRecordPo;
import com.micro.frog.fortune.dao.mapper.CwlRecordMapper;
import com.micro.frog.fortune.sdk.cwl.CwlApi;
import com.micro.frog.fortune.sdk.cwl.model.CwlApiReq;
import com.micro.frog.fortune.sdk.cwl.model.CwlApiResp;
import com.micro.frog.fortune.sdk.cwl.model.CwlApiResp.ResultDTO;
import com.micro.frog.fortune.model.base.PageResp;
import com.micro.frog.fortune.model.cwl.CwlRecordReq;
import com.micro.frog.fortune.model.cwl.CwlRecordResp;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 历史记录
 * @author jason.lv
 */
@Slf4j
@Service
public class CwlRecordExecutor {

  @Autowired
  private CwlRecordMapper recordMapper;

  public PageResp<CwlRecordResp> execute(CwlRecordReq request) {

    QueryWrapper<CwlRecordPo> wrapper = new QueryWrapper<>();
    wrapper.eq("type", request.getName());


    IPage<CwlRecordPo> page = new Page<>(request.getPage(), request.getSize());
    page = recordMapper.selectPage(page, wrapper);

    List<CwlRecordResp> result = new ArrayList<>();
    if (CollectionUtil.isNotEmpty(page.getRecords())){
      for (CwlRecordPo record : page.getRecords()) {
        CwlRecordResp listResp = new CwlRecordResp();
        listResp.setCode(record.getCode());
        listResp.setName(record.getName());
        listResp.setDate(record.getDate());
        listResp.setRed(record.getRed());
        listResp.setBlue(record.getBlue());
        result.add(listResp);
      }
    }

    return PageResp.generate(result, page.getCurrent(), page.getSize(), page.getTotal());
  }

}

